﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Data.SqlClient;
using GeneralLibs;
using CustomSecurity;

namespace ListForms
{
    public partial class FrmEditFunction : ListForms.FrmBaseEditForm
    {
        public FrmEditFunction(FunctionForEdit Function)
        {
            InitializeComponent();
            _DoiTuong = Function;
        }

        protected override void FillData()
        {
            cmbParentFunctionID.DataSource = DanhMucFunctionForEdit.Instance.DataView;
            cmbParentFunctionID.DisplayColumn = "Name";
            cmbParentFunctionID.ValueColumn = "ID";
            cmbParentFunctionID.AddVisibleColumn("ID", "FunctionID");
            cmbParentFunctionID.AddVisibleColumn("Name", "Tên chức năng");

            if (_DoiTuong == null)
            {
                try
                {
                    cmbParentFunctionID.Focus();
                    this.Text = "Thêm mới chức năng";
                    _DoiTuong = DanhMucFunctionForEdit.Instance.NewDoiTuong();
                    cmbParentFunctionID.EditValue = Guid.Empty;
                    txtName.EditValue = "";
                    txtDescription.EditValue = "";
                    txtPosibleRight.EditValue = (int)0;

                    _EditState = EditState.CreatingNew;
                }
                catch (SqlException sqlEx)
                {
                    throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                try
                {
                    cmbParentFunctionID.Focus();

                    #region Doc du lieu

                    this.Text = "Sửa chức năng";
                    try
                    {
                        txtID.EditValue = (_DoiTuong as FunctionForEdit).ID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbParentFunctionID.EditValue = (_DoiTuong as FunctionForEdit).ParentFunctionID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtName.Text = (_DoiTuong as FunctionForEdit).Name;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtDescription.Text = (_DoiTuong as FunctionForEdit).Description;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtPosibleRight.EditValue = (_DoiTuong as FunctionForEdit).PosibleRight;
                    }
                    catch (Exception ex)
                    { }
                  
                    #endregion
                }
                catch (Exception ex)
                {
                }
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtName.Text == "") throw new CustomException("Chưa điền tên chức năng", ExceptionType.LackOfInputInfo);
            if (txtDescription.EditValue == null) throw new CustomException("Chưa điền mô tả.", ExceptionType.LackOfInputInfo);
            if (txtPosibleRight.EditValue == null ) throw new CustomException("Chưa điền quyền của Form", ExceptionType.LackOfInputInfo);
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            try
            {
                    #region Set value
                    //buoc phai co
                    (_DoiTuong as FunctionForEdit).Name = txtName.Text.Trim();
                    (_DoiTuong as FunctionForEdit).Description = txtDescription.Text.Trim();
                    (_DoiTuong as FunctionForEdit).PosibleRight = Convert.ToInt32(txtPosibleRight.EditValue);
                    
                    //khong dien cung duoc
                    (_DoiTuong as FunctionForEdit).ParentFunctionID = (Guid)cmbParentFunctionID.EditValue;
                    #endregion
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
